const cacheUtil={
	setCookie:(name,value,{maxAge,domain,path,secure}={})=>{
		let cookieText=`${encodeURIComponent(name)}=${encodeURIComponent(value)}`
		if(typeof maxAge=='number'){
			cookieText+=`; max-age=${maxAge}`
		}
		if(domain){
			cookieText+=`; domin=${domain}`
		}
		if(path){
			cookieText+=`; path=${path}`
		}
		if(secure){
			cookieText+=`; secure=${secure}`
		}
		document.cookie=cookieText
	},
	getCookie:(name)=>{
		if(name){
			const cookieList=document.cookie.split('; ')
			for(let val of cookieList){
				const [cookieName,cookieVal]=[val.split('=')[0],val.split('=')[1]]
				if(name==decodeURIComponent(cookieName)){
					return decodeURIComponent(cookieVal)
				}
			}
		}
		return ''
	},
	
	// removeCookie:(name)=>{
	// 	setCookie(name,'',{maxAge:-1})
	// },
	
	setLocalStorage:(key,val)=>{
		if(key&&val){
			localStorage.setItem(key,JSON.stringify(val))
		}
	},
	
	getLocalStorage:(key)=>{
		if(key){
			const jsonStr=localStorage.getItem(key)
			return JSON.parse(jsonStr)
		}
	}
}

export default cacheUtil